package com.resou.reader.bookstore.more;

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.resou.reader.R;
import com.resou.reader.data.bookstore.model.MorePlateInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MoreAdapter extends RecyclerView.Adapter<MoreAdapter.MoreHolder> {

    private List<MorePlateInfo> mInfoList;

    public MoreAdapter() {
        mInfoList = new ArrayList<>();
    }

    private OnMoreAdapterInteractionListener mInteractionListener;

    public void setInteractionListener(OnMoreAdapterInteractionListener interactionListener) {
        mInteractionListener = interactionListener;
    }

    public void setInfoList(List<MorePlateInfo> infoList) {
        mInfoList = infoList;
        notifyDataSetChanged();
    }

    private static final String TAG = "MoreAdapter";

    public void addInfoList(List<MorePlateInfo> infoList) {
        int lastSize = getItemCount();
        mInfoList.addAll(infoList);
        notifyItemRangeInserted(lastSize, infoList.size());
    }

    @NonNull
    @Override
    public MoreHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_type_hot_search_item, parent, false);
        return new MoreHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MoreHolder holder, int position) {
        MorePlateInfo info = mInfoList.get(position);
        RequestOptions options = new RequestOptions();
        options.format(DecodeFormat.PREFER_RGB_565);
        Glide.with(holder.itemView.getContext()).load(info.getNovelCover()).apply(options).into(holder.ivCover);
        holder.tvAuthor.setText(info.getNovelAuthor());
        holder.tvBookIntro.setText(info.getNovelSummary());
        holder.tvBookName.setText(info.getNovelName());
        //只有一个标签
        holder.tvTag2.setText(info.getNovelCategoryName());
        holder.itemView.setOnClickListener(v -> {
            mInteractionListener.onItemClicked(info.getNovelId());
        });
    }

    @Override
    public int getItemCount() {
        return mInfoList == null ? 0 : mInfoList.size();
    }

    static class MoreHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.card_view)
        CardView cardView;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_intro)
        TextView tvBookIntro;
        @BindView(R.id.iv_author_icon)
        ImageView ivAuthorIcon;
        @BindView(R.id.tv_author)
        TextView tvAuthor;
        @BindView(R.id.tv_tag1)
        TextView tvTag1;
        @BindView(R.id.tv_tag2)
        TextView tvTag2;

        public MoreHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvTag1.setVisibility(View.GONE);
        }
    }

    interface OnMoreAdapterInteractionListener {
        void onItemClicked(String bookId);
    }
}
